from machine import Pin
from utime import sleep

#8*8点阵共阴点阵
class Lattice():
	def __init__(self,clk,cs,din,num=1):
		self.P_CLK = Pin(clk,Pin.OUT)
		self.P_CS = Pin(cs,Pin.OUT)
		self.P_DIN = Pin(din,Pin.OUT)
		
		self.font = FONT8_8
		self.num = num
		self.write_data(0x09, 0x00)#译码方式：BCD码
		self.write_data(0x0a, 0x0f)#亮度
		self.write_data(0x0b, 0x07)#扫描界限；8个数码管显示
		self.write_data(0x0c, 0x01)#掉电模式：0，普通模式：1
		self.write_data(0x0f, 0x01)#显示测试：1；测试结束，正常显示：0
			
	def write_byte(self,data):
		for i in range(8):
			self.P_CLK.value(0)
			self.P_DIN.value(data&0x80)
			data <<= 1
			self.P_CLK.value(1)

	def write_data(self,addr,dat):
		self.P_CS.value(0)
		for i in range(self.num):
			self.write_byte(addr)
			self.write_byte(dat)
		self.P_CS.value(1)
		sleep(1)

	def display(self,msg):
		for i in msg:
			if i in self.font.keys():
				DATA = self.font[i]
				for k,d in enumerate(DATA):
					self.write_data(k+1,d)
				sleep(1.5)


FONT8_8= {
	'0':[0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C],#0
	'1':[0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10],#1
	'2':[0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E],#2
	'3':[0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0],#3
	'4':[0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8],#4
	'5':[0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0],#5
	'6':[0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0],#6
	'7':[0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8],#7
	'8':[0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E],#8
	'9':[0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E],#9
	'A':[0x8,0x14,0x22,0x3E,0x22,0x22,0x22,0x22],#A
	'B':[0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x0],#B
	'C':[0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x0],#C
	'D':[0x7C,0x42,0x42,0x42,0x42,0x42,0x7C,0x0],#D
	'E':[0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x7C],#E
	'F':[0x7C,0x40,0x40,0x7C,0x40,0x40,0x40,0x40],#F
	'G':[0x3C,0x40,0x40,0x40,0x40,0x44,0x44,0x3C],#G
	'H':[0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0x44],#H
	'I':[0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x7C],#I
	'J':[0x3C,0x8,0x8,0x8,0x8,0x8,0x48,0x30],#J
	'K':[0x0,0x24,0x28,0x30,0x20,0x30,0x28,0x24],#K
	'L':[0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x7C],#L
	'M':[0x81,0xC3,0xA5,0x99,0x81,0x81,0x81,0x81],#M
	'N':[0x0,0x42,0x62,0x52,0x4A,0x46,0x42,0x0],#N
	'O':[0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C],#O
	'P':[0x3C,0x22,0x22,0x22,0x3C,0x20,0x20,0x20],#P
	'Q':[0x1C,0x22,0x22,0x22,0x22,0x26,0x22,0x1D],#Q
	'R':[0x3C,0x22,0x22,0x22,0x3C,0x24,0x22,0x21],#R
	'S':[0x0,0x1E,0x20,0x20,0x3E,0x2,0x2,0x3C],#S
	'T':[0x0,0x3E,0x8,0x8,0x8,0x8,0x8,0x8],#T
	'U':[0x42,0x42,0x42,0x42,0x42,0x42,0x22,0x1C],#U
	'V':[0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18],#V
	'W':[0x0,0x49,0x49,0x49,0x49,0x2A,0x1C,0x0],#W
	'X':[0x0,0x41,0x22,0x14,0x8,0x14,0x22,0x41],#X
	'Y':[0x41,0x22,0x14,0x8,0x8,0x8,0x8,0x8],#Y
	'Z':[0x0,0x7F,0x2,0x4,0x8,0x10,0x20,0x7F],#Z
	'大':[0x00,0x66,0xFF,0xFF,0x7E,0x3C,0x18,0x00],#大心
	'小':[0x00,0x00,0x66,0x7E,0x3C,0x18,0x00,0x00],#小心
	'中':[0x8,0x7F,0x49,0x49,0x7F,0x8,0x8,0x8],#中
	'国':[0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE],#国
}



#CLK -> X1,CS -> X2,DIN -> X3
#num级联的模块数量
led = Lattice(14, 27, 13,num=1)
#要显示的内容,显示的内容根据字库文件而定，见font_max7219.py。
#大家也可以自定义图案添加上。
msg = 'L'

while True:
	#显示
	led.display(msg)
	
# import MAX7219 #导入模块
# from pyb import UART
# #初始化串口参数，使用串口6 波特率9600 超时时间50毫秒
# #串口6 TX->Y1 RX->Y2 
# #其他串口对应的引脚编号详见针脚图：http://old.tpyboard.com/document/documents/10x/TPYBoardv10xPCBpng.pdf
# uart = UART(6,9600,timeout=50)

# #CLK -> X1,CS -> X2,DIN -> X3
# #num级联的模块数量
# led = MAX7219.Lattice('X1','X2','X3',num=1)
# led.display('大')                   #默认显示大心图案
# while True:
	# if uart.any() > 0:              #any()返回串口缓存区的数据长度，返回值大于0即表示有数据
		# DATA = uart.read().decode() #读取缓存区全部数据，返回值为bytes，decode()转为字符串
		# led.display(DATA)           #进行显示
